﻿using System;
using System.Collections.Generic;
using System.Text;
using ComponentFactory.Krypton.Toolkit;
using System.Windows.Forms;
using System.IO;
using ModelPredictPro.WindowForms.AppCodes;

namespace ModelPredictPro.WindowForms {

    public class TextBoxFileName : KryptonTextBox {
        public string FileNotExistText { get; set; }
        public string FileDirectoryNotExistText { get; set; }

        public OpenFileDialog OpenFileDialog { get; set; }
        ButtonSpecAny openFileButton;
        public ButtonSpecAny OpenFileButton {
            get { return openFileButton; }
        }
        public ErrorProvider ErrorProviderControl { get; set; }

        public TextBoxFileName()
            : base() {
            this.AllowDrop = true;
            this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.AutoCompleteSource = AutoCompleteSource.AllSystemSources;
            this.FileNotExistText = @"File not exist, please check !";
            this.FileDirectoryNotExistText = @"Directory of output file not exist, please check !";
            AddButton();
        }

        private void AddButton() {
                openFileButton = new ButtonSpecAny();
                openFileButton.Text = "...";
                openFileButton.Click += new EventHandler(openFileButton_Click);
                this.ButtonSpecs.Add(openFileButton);
        }

        void openFileButton_Click(object sender, EventArgs e) {
            if (OpenFileDialog == null)
                OpenFileDialog = new OpenFileDialog();
            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
                this.Text = OpenFileDialog.FileName;
        }

        #region Check File Path

        public virtual bool CheckFileExist() {
            this.Text = this.Text.Trim();
            bool rst = File.Exists(Text);
            SetErrorInfo(rst, FileNotExistText);
            return rst;
        }
        public virtual bool CheckFileDirectoryExist() {
            this.Text = this.Text.Trim();
            string dir = GetDir(Text);
            bool  rst = Directory.Exists(dir);
            SetErrorInfo(rst, FileDirectoryNotExistText);
            return rst;
        }
        public virtual bool CheckInputPattern(string pattern, string errorInfo) {
            this.Text = this.Text.Trim();
            bool rst = System.Text.RegularExpressions.Regex.IsMatch(Text, pattern);
            SetErrorInfo(rst, errorInfo);
            return rst;
        }
        protected string GetDir(string path) {
            int index = path.LastIndexOf(Path.DirectorySeparatorChar);
            if (index < 0) { return ""; }
            return path.Substring(0, index);
        }
        public virtual bool CheckMultiFileExist(out string []fileNames) {
            bool rst = FileHelper.IsBatchFile(Text, out fileNames);
            SetErrorInfo(rst,  @"Please input a multifile name. e.g. C:\train*.txt");
            return rst;
        }
        public virtual bool CheckMultiFileTemplate() {
            bool rst = Text.Contains("{1}");
            SetErrorInfo(rst, "Please input multifile template, {0} for origin directory, {1} for file name, {2} for extension.  e.g. {0}\\{1}.new");
            return rst;
        }
        public virtual string GetRealFilePath(string originFileName) {
            return FileHelper.GetOutputPath(originFileName, Text);
        }
        #endregion

        protected virtual void SetErrorInfo(bool noError, string info) {
            if (ErrorProviderControl == null)
                return;
            if (noError)
                ErrorProviderControl.SetError(this, "");
            else
                ErrorProviderControl.SetError(this, info);
        }

        #region File drag

        protected override void OnDragEnter(DragEventArgs drgevent) {
            base.OnDragEnter(drgevent);
            drgevent.Effect = DragDropEffects.All;
        }
        protected override void OnDragDrop(DragEventArgs drgevent) {
            base.OnDragDrop(drgevent);
            string[] fileNames = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
            if (fileNames.Length > 0) {
                this.Text = fileNames[0];
            }
        }

        #endregion

    }

}
